{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 机器学习练习-降维\n",
    "\n",
    "代码更新地址：https://github.com/fengdu78/WZU-machine-learning-course\n",
    "\n",
    "代码修改并注释：黄海广，haiguang2000@wzu.edu.cn "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Principal component analysis（主成分分析）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "PCA是在数据集中找到“主成分”或最大方差方向的线性变换。 它可以用于降维。 在本练习中，我们首先负责实现PCA并将其应用于一个简单的二维数据集，以了解它是如何工作的。 我们从加载和可视化数据集开始。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sb\n",
    "from scipy.io import loadmat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>X1</th>\n",
       "      <th>X2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3.381563</td>\n",
       "      <td>3.389113</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.527875</td>\n",
       "      <td>5.854178</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.655682</td>\n",
       "      <td>4.411995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2.765235</td>\n",
       "      <td>3.715414</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.846560</td>\n",
       "      <td>4.175506</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         X1        X2\n",
       "0  3.381563  3.389113\n",
       "1  4.527875  5.854178\n",
       "2  2.655682  4.411995\n",
       "3  2.765235  3.715414\n",
       "4  2.846560  4.175506"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv('data/pcadata.csv')\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = data.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAArkAAAHSCAYAAADohdOwAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAblUlEQVR4nO3dQYik6Xkf8P+r3jYuKxYN0cRoeqPs+tI5aLBaDIIwIBLJcdtYiGFOCjgQ57CXYGQCbXZyCc4hM9AQnJNhkWIUrNg48qgP2kRtwUQoCrHErFpyx5b6ECNh1cTZEaGx5RTRMH5zmO4ZzWzvdFVPVX31vfX7wbIzXzddz24X1L++ep7nLbXWAABAS97RdQEAADBtQi4AAM0RcgEAaI6QCwBAc4RcAACaI+QCANCcF2bxQ9/97nfXl156aRY/GgAAkiRvvPHG92utF0772kxC7ksvvZQ7d+7M4kcDAECSpJTy3bf7mnYFAACaI+QCANAcIRcAgOYIuQAANEfIBQCgOUIuAADNEXIBAGiOkAsAQHO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LP1FpvdV0P3Tn+eOpLSX6+20qYoytJPnbck/m7ST5cSvntbkti3mqtd4///WaSzyX5YLcVMWffS/K9H/kU77N5GHpnQshlLo6Hjj6V5Fu11n/TdT3MXynlQill7fjPgyQ/m+TbnRbF3NRar9daX6y1vpTk40lu11p/qeOymKNSyjuPB49z/BH1zyWxcWmJ1Fr/PMmflVI2ji99JMnMBtBfmNUPflop5XeS/P0k7y6lfC/Jv6y1fmpej0/nriT5x0kOjnsyk+Rf1Fr/U3clMWfvSfLpUspKHr7B/r1aqzVSsDx+KsnnHt7zyAtJ/kOt9QvdlkQHfiXJZ443K/xpkl+e1QM58QwAgOZoVwAAoDlCLgAAzRFyAQBojpALAEBzhFwAAJoj5AIA0BwhFwCA5gi5AAA05/8DlO+xSBCccGIAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 864x576 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots(figsize=(12,8))\n",
    "ax.scatter(X[:, 0], X[:, 1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "PCA的算法相当简单。 在确保数据被归一化之后，输出仅仅是原始数据的协方差矩阵的奇异值分解。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pca(X):\n",
    "    # normalize the features\n",
    "    X = (X - X.mean()) / X.std()\n",
    "\n",
    "    # compute the covariance matrix\n",
    "    X = np.matrix(X)\n",
    "    cov = (X.T * X) / X.shape[0]\n",
    "\n",
    "    # perform SVD\n",
    "    U, S, V = np.linalg.svd(cov)\n",
    "\n",
    "    return U, S, V"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(matrix([[-0.79241747, -0.60997914],\n",
       "         [-0.60997914,  0.79241747]]),\n",
       " array([1.43584536, 0.56415464]),\n",
       " matrix([[-0.79241747, -0.60997914],\n",
       "         [-0.60997914,  0.79241747]]))"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "U, S, V = pca(X)\n",
    "U, S, V"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在我们有主成分（矩阵U），我们可以用这些来将原始数据投影到一个较低维的空间中。 对于这个任务，我们将实现一个计算投影并且仅选择顶部K个分量的函数，有效地减少了维数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def project_data(X, U, k):\n",
    "    U_reduced = U[:,:k]\n",
    "    return np.dot(X, U_reduced)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[-4.74689738],\n",
       "        [-7.15889408],\n",
       "        [-4.79563345],\n",
       "        [-4.45754509],\n",
       "        [-4.80263579],\n",
       "        [-7.04081342],\n",
       "        [-4.97025076],\n",
       "        [-8.75934561],\n",
       "        [-6.2232703 ],\n",
       "        [-7.04497331],\n",
       "        [-6.91702866],\n",
       "        [-6.79543508],\n",
       "        [-6.3438312 ],\n",
       "        [-6.99891495],\n",
       "        [-4.54558119],\n",
       "        [-8.31574426],\n",
       "        [-7.16920841],\n",
       "        [-5.08083842],\n",
       "        [-8.54077427],\n",
       "        [-6.94102769],\n",
       "        [-8.5978815 ],\n",
       "        [-5.76620067],\n",
       "        [-8.2020797 ],\n",
       "        [-6.23890078],\n",
       "        [-4.37943868],\n",
       "        [-5.56947441],\n",
       "        [-7.53865023],\n",
       "        [-7.70645413],\n",
       "        [-5.17158343],\n",
       "        [-6.19268884],\n",
       "        [-6.24385246],\n",
       "        [-8.02715303],\n",
       "        [-4.81235176],\n",
       "        [-7.07993347],\n",
       "        [-5.45953289],\n",
       "        [-7.60014707],\n",
       "        [-4.39612191],\n",
       "        [-7.82288033],\n",
       "        [-3.40498213],\n",
       "        [-6.54290343],\n",
       "        [-7.17879573],\n",
       "        [-5.22572421],\n",
       "        [-4.83081168],\n",
       "        [-7.23907851],\n",
       "        [-4.36164051],\n",
       "        [-6.44590096],\n",
       "        [-2.69118076],\n",
       "        [-4.61386195],\n",
       "        [-5.88236227],\n",
       "        [-7.76732508]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Z = project_data(X, U, 1)\n",
    "Z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们也可以通过反向转换步骤来恢复原始数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def recover_data(Z, U, k):\n",
    "    U_reduced = U[:,:k]\n",
    "    return np.dot(Z, U_reduced.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[3.76152442, 2.89550838],\n",
       "        [5.67283275, 4.36677606],\n",
       "        [3.80014373, 2.92523637],\n",
       "        [3.53223661, 2.71900952],\n",
       "        [3.80569251, 2.92950765],\n",
       "        [5.57926356, 4.29474931],\n",
       "        [3.93851354, 3.03174929],\n",
       "        [6.94105849, 5.3430181 ],\n",
       "        [4.93142811, 3.79606507],\n",
       "        [5.58255993, 4.29728676],\n",
       "        [5.48117436, 4.21924319],\n",
       "        [5.38482148, 4.14507365],\n",
       "        [5.02696267, 3.8696047 ],\n",
       "        [5.54606249, 4.26919213],\n",
       "        [3.60199795, 2.77270971],\n",
       "        [6.58954104, 5.07243054],\n",
       "        [5.681006  , 4.37306758],\n",
       "        [4.02614513, 3.09920545],\n",
       "        [6.76785875, 5.20969415],\n",
       "        [5.50019161, 4.2338821 ],\n",
       "        [6.81311151, 5.24452836],\n",
       "        [4.56923815, 3.51726213],\n",
       "        [6.49947125, 5.00309752],\n",
       "        [4.94381398, 3.80559934],\n",
       "        [3.47034372, 2.67136624],\n",
       "        [4.41334883, 3.39726321],\n",
       "        [5.97375815, 4.59841938],\n",
       "        [6.10672889, 4.70077626],\n",
       "        [4.09805306, 3.15455801],\n",
       "        [4.90719483, 3.77741101],\n",
       "        [4.94773778, 3.80861976],\n",
       "        [6.36085631, 4.8963959 ],\n",
       "        [3.81339161, 2.93543419],\n",
       "        [5.61026298, 4.31861173],\n",
       "        [4.32622924, 3.33020118],\n",
       "        [6.02248932, 4.63593118],\n",
       "        [3.48356381, 2.68154267],\n",
       "        [6.19898705, 4.77179382],\n",
       "        [2.69816733, 2.07696807],\n",
       "        [5.18471099, 3.99103461],\n",
       "        [5.68860316, 4.37891565],\n",
       "        [4.14095516, 3.18758276],\n",
       "        [3.82801958, 2.94669436],\n",
       "        [5.73637229, 4.41568689],\n",
       "        [3.45624014, 2.66050973],\n",
       "        [5.10784454, 3.93186513],\n",
       "        [2.13253865, 1.64156413],\n",
       "        [3.65610482, 2.81435955],\n",
       "        [4.66128664, 3.58811828],\n",
       "        [6.1549641 , 4.73790627]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_recovered = recover_data(Z, U, 1)\n",
    "X_recovered"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAsIAAAHTCAYAAAAtcH56AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAiXUlEQVR4nO3dfYylZ3km+OumXKMcCFFFck9wl208K6GORFrQTMuALEXAZOlAGNLqQTtklUSTfzywaAWaqCM6GpGwGqmzam1EJkhYXrK7QSHJsKGpJYxJBwnQQCQTtV02jWO3xGaZwdXsuMOmMF6OZsvFs3/0KdNdrur66PNZ7+8nHfU5z3nr1C0d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k+QLrTXffTd8JMlvJPnhhOtg/FqSv6yqR6rq/kkXw9j8V0muJvlfBy1RH6+ql026KMbq3Un+ZJy/cKaCcFX9eJJPJ/lAa+3ZSdfDeLTW1ltrr01yZ5J7q+pnJlwSI1ZV70jyTGvtkUnXwkTc11p7XZK3JXlfVf3spAtiLG5L8rokH2utHUvy/yb54GRLYlwGrTDvTPK/j/P3zkwQHvSHfjrJJ1tr5yddD+M3+BPZl5P8/GQrYQzuS/LOQa/onyZ5S1X90WRLYlxaa1cG/z6T5DNJ7p1sRYzJ00mevu6vfn+Wa8GYbnhbkkdba/95nL90JoLwYMPUHyR5srX2u5Ouh/GpqkNVtTB43kvyc0memmhRjFxr7Uxr7c7W2j259qeyL7bWfnnCZTEGVfWywaboDP4s/tYk35hsVYxDa+3/TvLtqjoyWPonSWyK745fypjbIpJrf4aYBfcl+ZUklwa9oknym621hyZXEmNyR5I/HOwkfUmST7XWjNKCg+unknzm2v2P3Jbkj1trfzHZkhij/z7JJwd/Jv/bJL824XoYg6p6aZL/Osm/HPvvnoXxaQAAMGwz0RoBAADDJggDANBJgjAAAJ0kCAMA0EmCMAAAnSQIAwDQSYIwAACdJAgDANBJ/z98R8QNDwWnXgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 864x576 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots(figsize=(12,8))\n",
    "ax.scatter(list(X_recovered[:, 0]), list(X_recovered[:, 1]))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请注意，第一主成分的投影轴基本上是数据集中的对角线。 当我们将数据减少到一个维度时，我们失去了该对角线周围的变化，所以在我们的再现中，一切都沿着该对角线。\n",
    "\n",
    "我们在此练习中的最后一个任务是将PCA应用于脸部图像。 通过使用相同的降维技术，我们可以使用比原始图像少得多的数据来捕获图像的“本质”。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5000, 1024)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faces = loadmat('data/ex7faces.mat')\n",
    "X = faces['X']\n",
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_n_image(X, n):\n",
    "    \"\"\" plot first n images\n",
    "    n has to be a square number\n",
    "    \"\"\"\n",
    "    pic_size = int(np.sqrt(X.shape[1]))\n",
    "    grid_size = int(np.sqrt(n))\n",
    "\n",
    "    first_n_images = X[:n, :]\n",
    "\n",
    "    fig, ax_array = plt.subplots(nrows=grid_size,\n",
    "                                 ncols=grid_size,\n",
    "                                 sharey=True,\n",
    "                                 sharex=True,\n",
    "                                 figsize=(8, 8))\n",
    "\n",
    "    for r in range(grid_size):\n",
    "        for c in range(grid_size):\n",
    "            ax_array[r, c].imshow(first_n_images[grid_size * r + c].reshape(\n",
    "                (pic_size, pic_size)))\n",
    "            plt.xticks(np.array([]))\n",
    "            plt.yticks(np.array([]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "练习代码包括一个将渲染数据集中的前100张脸的函数。 而不是尝试在这里重新生成，您可以在练习文本中查看他们的样子。 我们至少可以很容易地渲染一个图像。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "face = np.reshape(X[3,:], (32, 32))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD5CAYAAADhukOtAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAclUlEQ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6RdFHAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(face)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "看起来很糟糕。 这些只有32 x 32灰度的图像（它也是侧面渲染，但我们现在可以忽略）。 我们的下一步是在面数据集上运行PCA，并取得前100个主要特征。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "U, S, V = pca(X)\n",
    "Z = project_data(X, U, 100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在我们可以尝试恢复原来的结构并再次渲染。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD5CAYAAADhukOtAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAb5klEQVR4nO2da4ycZ3XH/2fue1+vbzG2iUMSqYS0hGgVRUqLaGlRGiEFPoCgEsqHCPOBSEWlH6JUKuk3WhUQHyokUyJCRYGogIiqqCWKiiKkKuCkieNgCLk4iWNnfVnbe5376YedtE54/mfXs7uzhuf/k1Y785553vfMM++Zd+b5zznH3B1CiN99ClvtgBBiMCjYhcgEBbsQmaBgFyITFOxCZIKCXYhMKK1nsJndDuCrAIoA/tndvxgebGjEy+NTl38g8pbkRT7ES1xStGJgM25zN7Kd+4FuesyqRMMCH62Pw22G+tqPH/3Sl//ktQQQzm8xOHfKhQ61DRVb1DZarCe3DxsfwzhxooPZ2fRJ13ewm1kRwD8B+DMAJwD83MwedvdfsDHl8Slc9xd/ddnHag+nt7cm+MS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Tt/kBDHhONqPI61YEe6rGxlZJAre5+80A/hzAZ83s/Vvkx5XE1wBci5UeAacAfGlQBzazUQDfB/A5d58b1HHX4MfA58TXUeSVsRXBfgLA/kvu7wNwcgv8gLuf7P0/DeCHWPmKsVWsqYDnZuPuM70TrQvg6xjQnJhZGSsB9m13/0Fv88DnJOXHVs1J79gXcJlFXhlbEew/B3C9mV1jZhUAn8BK8cqBYmYjZjb25m0AHwJwNB61qVwRBTzfPJl6fBQDmBMzMwDfAHDM3b98iWmgc8L8GPScbFqR10GtML5ttfEOrKx0vgjgb7bIh3dhRQl4BsBzg/QDwHew8nGwhZVPOncD2I6VNlq/7v2f2iI//gXAswCO9E6uPQPw4w+x8lXuCICne393DHpOAj8GOicA/gDA//SOdxTA3/a2r2s+9As6ITJBv6ATIhMU7EJkgoJdiExQsAuRCQp2ITJBwS5EJijYhcgEBbsQmfC/z3t1g2e/ql0AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "X_recovered = recover_data(Z, U, 100)\n",
    "face = np.reshape(X_recovered[3,:], (32, 32))\n",
    "plt.imshow(face)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以看到：数据维度减少，但细节并没有怎么损失。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 参考\n",
    "[1] Andrew Ng. Machine Learning[EB/OL]. StanfordUniversity,2014.https://www.coursera.org/course/ml\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
